/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package File;

import com.google.code.javascribd.connection.ScribdClient;
import com.google.code.javascribd.connection.ScribdConnectionException;
import com.google.code.javascribd.connection.StreamableData;
import com.google.code.javascribd.docs.Upload;
import com.google.code.javascribd.docs.UploadResponse;
import com.google.code.javascribd.type.Access;
import com.google.code.javascribd.type.ApiKey;
import com.google.code.javascribd.type.FileData;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Tiong Jia Ming
 */
public class Scribd {

    private ScribdClient client = new ScribdClient();
    private UploadResponse responseScribd = null;
    private ApiKey apiKey = new ApiKey("36u5bcyvy0kxjqi4zc671");

    public String getScribdURL(String extension, String fileID) throws Exception {
        HttpURLConnection connection = EstablishConnection("docs.getDownloadUrl", "&doc_id=" + fileID + "&doc_type=original");
        InputStream stream = connection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(stream));
        String haha = "";
        int end = 0;
        int start = 0;
        String link = "";
        while ((haha = br.readLine()) != null) {
            if (haha.contains("<![CDATA[")) {
                end = haha.indexOf("]]>");
                start = haha.lastIndexOf("<![CDATA[");
                link = haha.substring(start + 9, end);
            }
        }
        br.close();
        stream.close();
        return link;
    }

    public String viewOnlineDoc(String id) {
        String key = "";
        try {
            HttpURLConnection connection = EstablishConnection("docs.getList", "");
            InputStream stream = connection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(stream));
            String haha = "";
            int start = 0;
            int end = 0;
            String fileID = id;
            String check = "";
            while ((haha = br.readLine()) != null) {
                if (haha.contains("<doc_id>") && haha.contains(fileID)) {
                    end = haha.indexOf("</doc_id>", 0);
                    start = haha.lastIndexOf("<doc_id>");
                    check = haha;
                } else if (haha.contains("<access_key>") && check.contains(fileID)) {
                    end = haha.indexOf("</access_key>", 0);
                    start = haha.lastIndexOf("<access_key>");
                    key = haha.substring(start + 12, end);
                    check = "";
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(Scribd.class.getName()).log(Level.SEVERE, null, ex);
        }
        return key;
    }

    public String getThumbnail(String fileID) {
        String link = "";
        try {
            HttpURLConnection connection = EstablishConnection("thumbnail.get","&doc_id=" + fileID);
            InputStream stream = connection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(stream));
            String haha = "";
            int end = 0;
            int start = 0;
            while ((haha = br.readLine()) != null) {
                if (haha.contains("<thumbnail_url>")) {
                    end = haha.indexOf("</thumbnail_url>");
                    start = haha.lastIndexOf("<thumbnail_url>");
                    link = haha.substring(start + 15, end);
                }
            }
            br.close();
            stream.close();
        } catch (Exception ex) {
            Logger.getLogger(Scribd.class.getName()).log(Level.SEVERE, null, ex);
        }
        return link;

    }

    public HttpURLConnection EstablishConnection(String method, String param) {
        HttpURLConnection connection = null;
        try {
            String URL = "http://api.scribd.com/api?method=" + method + "&api_key=" + apiKey + param;
            URL url = new URL(URL);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
        } catch (Exception ex) {
            Logger.getLogger(Scribd.class.getName()).log(Level.SEVERE, null, ex);
        }
        return connection;
    }

    public void ScribdUpload(File file, String fileName) {
        try {
            StreamableData uploadData = new FileData(file);
            Upload uploadFile = new Upload(apiKey, uploadData);
            int extension = fileName.indexOf(".");
            uploadFile.setDocType(fileName.substring(extension + 1));
            uploadFile.setAccess(Access.PRIVATE);
            responseScribd = client.execute(uploadFile);
        } catch (ScribdConnectionException ex) {
            Logger.getLogger(FileUpload.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FileUpload.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getDocID() {
        return responseScribd.getDocId().toString();
    }

    public String getKey() {
        return responseScribd.getAccessKey();
    }
}
